/*
 * @Author: yuluo
 * @Date: 2025-08-28
 * @LastEditors: yuluo
 * @LastEditTime: 2025-09-06
 * @FilePath: \Projecte:\gitee\GD32F427\gd32-f427-BootLoader\BSP\src\drv_timer.c
 * @Description: 
 * 如有特殊注意事项请填写
 * Copyright (c) by ${yuluo}, All Rights Reserved. 
 */

#include "main.h"

/**
 * @description: 定时器硬件初始化
 *              基本定时器的时钟来自APB1,当PPRE1 ≥ 2分频的时候
 *              基本定时器的时钟为APB1时钟的2倍, 而APB1为50M, 所以定时器TIMER6时钟 = 100Mhz
 *              定时器溢出时间计算方法: Tout = ((arr + 1) * (psc + 1)) / Ft us.
 *              Ft=定时器工作频率,单位:Mhz
 * @param {uint16_t} arr
 * @param {uint16_t} psc
 * @return {*}
 */
void drv_timer6_init(uint16_t arr, uint16_t psc)
{
    timer_parameter_struct timer6_initpara;
    /* 使能TIMER6时钟 */
    rcu_periph_clock_enable(RCU_TIMER6);
    /* 复位TIMER6 */
    timer_deinit(TIMER6);
    /* 初始化为默认值 */
    timer_struct_para_init(&timer6_initpara);
    /* 配置TIMER6 */
    timer6_initpara.prescaler           = psc - 1;          /* 分频值 */
    timer6_initpara.period              = arr - 1;          /* 装载值 */
    timer6_initpara.counterdirection    = TIMER_COUNTER_UP; /* 计数放式 */
    timer6_initpara.clockdivision       = TIMER_CKDIV_DIV1; /* 时钟分频 */
    timer_init(TIMER6, &timer6_initpara);
    /* 使能中断 */
    timer_interrupt_enable(TIMER6, TIMER_INT_UP);           /* 使能更新中断 */
    /* 配置优先级 */
    nvic_irq_enable(TIMER6_IRQn, 1, 3);
    /* 使能定时器*/
    timer_enable(TIMER6);
}

/**
 * @description: 
 * @return {*}
 */
void TIMER6_IRQHandler(void)
{
    /* 更新中断是否发生 */
    if(timer_interrupt_flag_get(TIMER6, TIMER_INT_FLAG_UP) == SET)
    {
        /* IO库时基 */
        io_tick_inc();
        /* 每隔10ms系统时间线++ */
        sys_timer.System_Timeline ++;
        /* 10ms任务标志 */
        sys_timer.System_10ms = 1;
        /* 清除更新中断*/
        timer_interrupt_flag_clear(TIMER6, TIMER_INT_FLAG_UP);
    }
}
